package ru.st.selenium;

import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import ru.st.selenium.model.Film;
import ru.st.selenium.model.User;
import ru.st.selenium.pages.PageManager;

public class AddFilm extends ru.st.selenium.pages.TestBase {

	@BeforeMethod
	public void login() throws Exception {
		if (!app.getUserHelper().isLoggedIn()){
			User user = new User().setLogin("admin").setPassword("admin");
			app.getUserHelper().loginAs(user);
		}
		app.getNavigationHelper().openMainPage();
	}

	@Test
	public void AddFilmOK() throws Exception {
		Film film = new Film().setTitle("Lord of the Rings - 2 Towers")
				.setYear("2002");
		app.getFilmHelper().create(film);
		
		final PageManager pageManager = app.getFilmHelper().getPageManager();
		if (!pageManager.filmInfoPage.isFilmAdded()) {
			throw new RuntimeException();
		}
	}

	@Test
	public void AddFilmWrong() throws Exception {
		Film film = new Film()
				.setYear("");
		app.getFilmHelper().create(film);
		
		final PageManager pageManager = app.getFilmHelper().getPageManager();
		if (!pageManager.addFilmPage.isErrorPresent()) {
			throw new RuntimeException("No error");
		} else if (pageManager.addFilmPage.getErrorsCount() > 1){
			throw new RuntimeException("More than one error: "+pageManager.addFilmPage.getErrorsCount());
		}
	}
}
